package com.example.work3.service;

import com.example.work3.entity.Student;
import com.example.work3.entity.User;
import com.example.work3.repository.StudentRepository;
import com.example.work3.repository.UserRepository;
import com.example.work3.tools.Md5;
import com.example.work3.tools.UniqueIdentifierGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class StudentService {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private UserRepository userRepository;
    //电话重复
    public Boolean isExistPhone(String phone){
        return studentRepository.existsByPhone(phone);
    }
    //注册账号
    public User addStudent(Student student){
        String t_id=student.getMajor().getId()+student.getEnrollmentDate().substring(0,4)+ UniqueIdentifierGenerator.generateUniqueIdentifier();
        student.setId(Integer.parseInt(t_id));
        System.out.println(student);
        Student save = studentRepository.save(student);
        if (save == null) return null;
        else {
            String s = Md5.md5Encrypt("123456");
            userRepository.save(new User(String.valueOf(save.getId()),s,0));
            return new User(String.valueOf(save.getId()),"123456",0);
        }
    }
    //查询学生
    public Student getStudentById(int id){
        return studentRepository.findById(id).orElse(null);
    }

}
